/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pb.diplom.first.gui.producerCategoryCommodity;

import com.pb.diplom.first.business.DbStorageProducer;
import com.pb.diplom.first.tables.Producer;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Slava
 */
public class ProducerTabPanel extends JPanel {

    private final DbStorageProducer storage = DbStorageProducer.getInstance();
    private JTable table;
    private AddOrUpdateProducerPanel addPanel;

    public ProducerTabPanel() {
        super(new GridLayout(2, 0));
        createUi();
        initData();
        initListeners();
    }

    private void createUi() {
        table = new JTable(new DefaultTableModel(new Object[]{"Id", "Name"}, 0) {
            @Override
            public boolean isCellEditable(int rowIndex, int mColIndex) {
                return false;
            }
        });
        table.getColumnModel().getColumn(0).setMaxWidth(50);
        addPanel = new AddOrUpdateProducerPanel();
        this.add(new JScrollPane(table));
        this.add(addPanel);
    }

    private void initData() {
        try {
            DefaultTableModel model = (DefaultTableModel) table.getModel();
            model.setRowCount(0);
            for (Object[] row : DbStorageProducer.getInstance().getProducersAsTable()) {
                model.addRow(row);
            }
        } catch (SQLException ex) {
            throw new RuntimeException("Error on reading data from db", ex);
        }
    }

    private void initListeners() {
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    int row = table.getSelectedRow();
                    Long id = (Long) table.getModel().getValueAt(row, 0);
                    String name = (String) table.getModel().getValueAt(row, 1);
                    addPanel.setProducer(new Producer(id, name));
                }
            }
        });
        addPanel.addSaveBtnListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Producer producer = null;
                try {
                    producer = storage.saveOrUpdate(addPanel.getProducer());
                } catch (SQLException ex) {
                    Logger.getLogger(ProducerTabPanel.class.getName()).log(Level.SEVERE, null, ex);
                }
                addPanel.clear();
                addOrUpdateTableRow(producer);
            }
        });
    }

    private void addOrUpdateTableRow(Producer producer) {
        for (int i = 0; i < table.getRowCount(); i++) {
            if (table.getValueAt(i, 0).equals(producer.getId())) {
                table.setValueAt(producer.getName(), i, 1);
                return;
            }
        }
        ((DefaultTableModel) table.getModel()).addRow(new Object[]{producer.getId(), producer.getName()});
    }

}
